package com.teamcenter.rac.massupdate.ui.grid;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.window.DefaultToolTip;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;

import com.teamcenter.rac.massupdate.ui.grid.layers.MassUpdateBodyLayerStack;
import com.teamcenter.rac.massupdate.ui.grid.layers.MassUpdateColumnHeaderLayerStack;
import com.teamcenter.rac.massupdate.ui.grid.layers.MassUpdateCornerLayerStack;
import com.teamcenter.rac.massupdate.ui.grid.layers.MassUpdateRowHeaderLayerStack;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.swt.TextWidgetMatcherEditor;
import net.sourceforge.nattable.NatTable;
import net.sourceforge.nattable.config.CellConfigAttributes;
import net.sourceforge.nattable.config.ConfigRegistry;
import net.sourceforge.nattable.config.DefaultNatTableStyleConfiguration;
import net.sourceforge.nattable.config.IConfigRegistry;
import net.sourceforge.nattable.data.IRowIdAccessor;
import net.sourceforge.nattable.edit.EditConfigAttributes;
import net.sourceforge.nattable.grid.layer.GridLayer;
import net.sourceforge.nattable.layer.DataLayer;
import net.sourceforge.nattable.layer.cell.CellDisplayConversionUtils;
import net.sourceforge.nattable.layer.cell.ColumnOverrideLabelAccumulator;
import net.sourceforge.nattable.layer.cell.LayerCell;
import net.sourceforge.nattable.selection.RowSelectionModel;
import net.sourceforge.nattable.selection.SelectionLayer;
import net.sourceforge.nattable.selection.config.DefaultSelectionLayerConfiguration;
import net.sourceforge.nattable.selection.config.RowOnlySelectionBindings;
import net.sourceforge.nattable.selection.config.RowOnlySelectionConfiguration;
import net.sourceforge.nattable.sort.config.SingleClickSortConfiguration;
import net.sourceforge.nattable.style.IStyle;
@SuppressWarnings({"rawtypes","unchecked"})
public class GridConfigurator<T extends IGridRow>
{
  private final GridModel m_gridModel;
  private final GridColumn[] m_columns;
  private final IRowIdAccessor<T> m_rowIdAccessor;
  private final Composite m_parent;
  private NatTable m_natTable;
  private final EventList<T> m_eventList;
  private FilterList<T> m_filterList;
  private IConfigRegistry m_configRegistry;
  private MassUpdateBodyLayerStack m_bodyLayerStack;
  private MassUpdateColumnHeaderLayerStack<T> m_columnHeaderLayerStack;
  private MassUpdateRowHeaderLayerStack m_rowHeaderLayerStack;
  private MassUpdateCornerLayerStack<T> m_cornerLayerStack;
  private GridLayer m_gridLayer;
  private Text m_filterTextBox;
  private TextFilterator<T> m_textFilterator;
  private SortedList<T> m_sortedList;
  public static final String BODY_COLUMN_LABEL_PREFIX = "bodyColLbl_";
  public static final String COLUMN_HEADER_COLUMN_LABEL_PREFIX = "colHdrLbl_";
  
  public GridConfigurator(Composite paramComposite, GridModel paramGridModel, EventList<T> paramEventList, IRowIdAccessor<T> paramIRowIdAccessor)
  {
    this.m_parent = paramComposite;
    this.m_gridModel = paramGridModel;
    this.m_rowIdAccessor = paramIRowIdAccessor;
    this.m_columns = paramGridModel.getColumns();
    this.m_eventList = GlazedLists.eventList(paramEventList);
  }
  
  public NatTable setupLayers()
  {
    this.m_configRegistry = new ConfigRegistry();
    this.m_sortedList = new SortedList(this.m_eventList, null);
    configureFilterList();
    this.m_bodyLayerStack = new MassUpdateBodyLayerStack(getGridColumns(), getFilterList());
    this.m_columnHeaderLayerStack = new MassUpdateColumnHeaderLayerStack(getGridModel(), getBodyLayerStack(), getSortedList(), getConfigRegistry());
    this.m_rowHeaderLayerStack = new MassUpdateRowHeaderLayerStack(this.m_bodyLayerStack);
    this.m_cornerLayerStack = new MassUpdateCornerLayerStack(this.m_columnHeaderLayerStack, this.m_rowHeaderLayerStack);
    this.m_gridLayer = new GridLayer(this.m_bodyLayerStack, this.m_columnHeaderLayerStack, this.m_rowHeaderLayerStack, this.m_cornerLayerStack);
    this.m_natTable = new NatTable(this.m_parent, this.m_gridLayer, false);
    this.m_natTable.setConfigRegistry(getConfigRegistry());
    this.m_natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    configureColumnProperties();
    configureSorting();
    configureSelectionStyle();
    configureTooltip();
    return this.m_natTable;
  }
  
  protected void configureFilterList()
  {
    if ((this.m_filterTextBox == null) || (this.m_textFilterator == null)) {
      this.m_filterList = new FilterList(getSortedList());
    } else {
      this.m_filterList = new FilterList(getSortedList(), new TextWidgetMatcherEditor(this.m_filterTextBox, this.m_textFilterator));
    }
  }
  
  protected void configureColumnProperties()
  {
    ColumnOverrideLabelAccumulator localColumnOverrideLabelAccumulator1 = getColumnHeaderLayerStack().getColumnOverrideLabelAccumulator();
    ColumnOverrideLabelAccumulator localColumnOverrideLabelAccumulator2 = getBodyLayerStack().getColumnLabelAccumulator();
    GridColumn[] arrayOfGridColumn = getGridColumns();
    DataLayer localDataLayer = getBodyLayerStack().getDataLayer();
    IConfigRegistry localIConfigRegistry = getConfigRegistry();
    for (int i = 0; i < arrayOfGridColumn.length; i++)
    {
      GridColumn localGridColumn = arrayOfGridColumn[i];
      localColumnOverrideLabelAccumulator1.registerColumnOverrides(i, new String[] { "colHdrLbl_" + i });
      localIConfigRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, localGridColumn.getColumnHeaderStyle(), "NORMAL", "colHdrLbl_" + i);
      Integer localInteger = localGridColumn.getWidth();
      if (localInteger != null) {
        localDataLayer.setColumnWidthByPosition(i, localInteger.intValue());
      }
      String str = "bodyColLbl_" + i;
      localColumnOverrideLabelAccumulator2.registerColumnOverrides(i, new String[] { str });
      if (localGridColumn.getDisplayConverter() != null) {
        localIConfigRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, localGridColumn.getDisplayConverter(), "NORMAL", str);
      }
      if (localGridColumn.getCellPainter() != null) {
        localIConfigRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, localGridColumn.getCellPainter(), "NORMAL", str);
      }
      IStyle localIStyle = localGridColumn.getColumnStyle();
      if (localIStyle != null) {
        localIConfigRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, localIStyle, "NORMAL", str);
      }
      if (localGridColumn.isEditable())
      {
        IEditorConfig localIEditorConfig = localGridColumn.getEditorConfig();
        localIConfigRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, localIEditorConfig.getEditableRule(), "NORMAL", str);
        localIConfigRegistry.registerConfigAttribute(EditConfigAttributes.DATA_VALIDATOR, localIEditorConfig.getValidator(), "NORMAL", str);
        localIConfigRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, localIEditorConfig.getCellEditor(), "EDIT", str);
      }
    }
  }
  
  protected void configureSorting()
  {
    this.m_natTable.addConfiguration(new SingleClickSortConfiguration());
  }
  
  protected void configureSelectionStyle()
  {
    SelectionLayer localSelectionLayer = this.m_bodyLayerStack.getSelectionLayer();
    if (this.m_gridModel.enableFullRowSelection)
    {
      localSelectionLayer.addConfiguration(new RowOnlySelectionConfiguration());
      this.m_natTable.addConfiguration(new RowOnlySelectionBindings());
      localSelectionLayer.setSelectionModel(new RowSelectionModel(localSelectionLayer, this.m_bodyLayerStack.getDataProvider(), this.m_rowIdAccessor));
    }
    else
    {
      localSelectionLayer.addConfiguration(new DefaultSelectionLayerConfiguration());
    }
  }
  
  protected DefaultToolTip configureTooltip()
  {
    DefaultToolTip local1 = new DefaultToolTip(getGrid(), 2, false)
    {
      protected String getText(Event paramAnonymousEvent)
      {
        NatTable localNatTable = GridConfigurator.this.getGrid();
        int i = localNatTable.getColumnPositionByX(paramAnonymousEvent.x);
        int j = localNatTable.getRowPositionByY(paramAnonymousEvent.y);
        LayerCell localLayerCell = localNatTable.getCellByPosition(i, j);
        if (localLayerCell != null)
        {
          String str = CellDisplayConversionUtils.convertDataType(localLayerCell, localNatTable.getConfigRegistry());
          if (str.length() > 0) {
            return str;
          }
        }
        return null;
      }
      
      protected Object getToolTipArea(Event paramAnonymousEvent)
      {
        NatTable localNatTable = GridConfigurator.this.getGrid();
        int i = localNatTable.getColumnPositionByX(paramAnonymousEvent.x);
        int j = localNatTable.getRowPositionByY(paramAnonymousEvent.y);
        return new Point(i, j);
      }
      
      protected boolean shouldCreateToolTip(Event paramAnonymousEvent)
      {
        String str = getText(paramAnonymousEvent);
        if (str != null)
        {
          if ((str.equalsIgnoreCase("true")) || (str.equalsIgnoreCase("false"))) {
            return false;
          }
          return super.shouldCreateToolTip(paramAnonymousEvent);
        }
        return false;
      }
    };
    local1.activate();
    return local1;
  }
  
  public MassUpdateBodyLayerStack getBodyLayerStack()
  {
    return this.m_bodyLayerStack;
  }
  
  public MassUpdateColumnHeaderLayerStack<T> getColumnHeaderLayerStack()
  {
    return this.m_columnHeaderLayerStack;
  }
  
  protected GridColumn[] getGridColumns()
  {
    return this.m_columns;
  }
  
  protected GridModel getGridModel()
  {
    return this.m_gridModel;
  }
  
  public NatTable getGrid()
  {
    return this.m_natTable;
  }
  
  public IConfigRegistry getConfigRegistry()
  {
    return this.m_configRegistry;
  }
  
  protected FilterList<T> getFilterList()
  {
    return this.m_filterList;
  }
  
  protected void setFilterList(FilterList<T> paramFilterList)
  {
    this.m_filterList = paramFilterList;
  }
  
  protected SortedList<T> getSortedList()
  {
    return this.m_sortedList;
  }
  
  protected Composite getParent()
  {
    return this.m_parent;
  }
  
  public EventList<T> getEventList()
  {
    return this.m_eventList;
  }
  
  public void setGridTextFilterDetails(Text paramText, TextFilterator paramTextFilterator)
  {
    Assert.isNotNull(paramText);
    Assert.isNotNull(paramTextFilterator);
    this.m_filterTextBox = paramText;
    this.m_textFilterator = paramTextFilterator;
  }
}

